package com.power.web;

import com.github.pagehelper.PageInfo;
import com.power.model.TActivity;
import com.power.query.ActivityQuery;
import com.power.result.R;
import com.power.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 海晨忆
 * @Des 活动
 * @Date 2025/8/13 18:59
 */
@RestController
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 用户列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/api/activitys")
    public R activityPage(@RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                          ActivityQuery activityQuery) {
        PageInfo<TActivity> userByPage = activityService.getActivityByPage(currentPage, pageSize, activityQuery);
        return R.OK(userByPage);
    }

    @PostMapping(value = "/api/activity")
    public R activityAdd(ActivityQuery activityQuery, @RequestHeader(value = "token") String token) {
        activityQuery.setToken(token);
        int saveCount = activityService.addActivity(activityQuery);
        return saveCount >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping(value = "/api/activity/{id}")
    public R activityDetail(@PathVariable(value = "id") Integer id) {
        TActivity tActivity = activityService.getActivityDetail(id);
        return R.OK(tActivity);
    }

    @PutMapping(value = "/api/activity")
    public R activityEdit(ActivityQuery activityQuery, @RequestHeader(value = "token") String token){
        activityQuery.setToken(token);
        int editCount = activityService.editActivity(activityQuery);
        return editCount >= 1 ? R.OK() : R.FAIL();
    }
}
